using System;
using UnityEngine;
namespace DanielSig.Geometry
{
	public static class SphereMath
	{	
		public static Vector3 ToAngles(Vector3 v)
		{
			float d = Mathf.Sqrt((v.x * v.x) + (v.z * v.z));
			Vector3 angles = new Vector3(Mathf.Atan2(v.y, d) * Mathf.Rad2Deg, Mathf.Atan2(v.z, v.x) * Mathf.Rad2Deg, 0);
			return FixAngles(angles);
		}
		public static Vector3 ToAngles(float x, float y, float z)
		{
			return ToAngles(new Vector3(x, y, z));
		}
		public static Vector3 ToSphericalCoordinates(Vector3 v)
		{
			Vector3 angleDistance = ToAngles(v);
			angleDistance.z = v.magnitude;
			return angleDistance;
		}
		public static Vector3 ToSphericalCoordinates(float x, float y, float z)
		{
			return ToSphericalCoordinates(new Vector3(x, y, z));
		}
		public static Vector3 ToVector(Vector3 ad)
		{
			if(ad.y > 180F)
			{
				ad.y -= 360F;
			}
			else if(ad.y < -180F)
			{
				ad.y += 360F;
			}
			if(ad.x > 90F)
			{
				ad.x -= 180F;
			}
			else if(ad.x < -90F)
			{
				ad.x += 180F;
			}
			ad.x *= Mathf.Deg2Rad;
			ad.y *= Mathf.Deg2Rad;
			return new Vector3(ad.z * Mathf.Sin(ad.y) * Mathf.Cos(ad.x), ad.z * Mathf.Sin(ad.x), ad.z * Mathf.Cos(ad.y) * Mathf.Cos(ad.x));
		}
		public static Vector3 ToVector(float xAngle, float yAngle, float distance)
		{
			return ToVector(new Vector3(xAngle, yAngle, distance));
		}
		public static float GetVectorX(float xAngle, float yAngle, float distance)
		{
			return distance * Mathf.Sin(yAngle * Mathf.Deg2Rad) * Mathf.Cos(xAngle * Mathf.Deg2Rad);
		}
		public static float GetVectorY(float xAngle, float yAngle, float distance)
		{
			return distance * Mathf.Sin(xAngle * Mathf.Deg2Rad);
		}
		public static float GetVectorZ(float xAngle, float yAngle, float distance)
		{
			return distance * Mathf.Cos(xAngle * Mathf.Deg2Rad) * Mathf.Cos(yAngle * Mathf.Deg2Rad);
		}
		public static Vector3 FixAngles(Vector3 v)
		{
			v.x = Mathf.Repeat(v.x, 180F);
			v.y = Mathf.Repeat(v.y, 360F);
			v.z = Mathf.Repeat(v.z, 360F);
			return v;
		}
	}
}

